***************************************************************************************************
// Data management: Intergenerational transmission of party affiliation within political families *
***************************************************************************************************

cls
clear all
clear matrix
set more off, perm
capture log close
log using "C:/Userdata/Shared/Logs/Parent politicians/datamanagement.text", replace text

cd "E:/ProjData/Parents politicians/"

* Macros
global scb "D:/SCB_ConPol/Stata"
global parties c fp kd m mp s v sd

********************************************************************************
// Define population
********************************************************************************

* Open multi-generational register
use "BioForaldrar.dta", clear

* Merge in party information for individual
merge 1:1 LopNr using "politicians.dta", nogen keep(1 3) keepusing(party firstyear tophalf partyshare_* modevaltyp)
rename party party_i
rename modevaltyp modevaltyp_i 

* Exclude those who have not run
keep if party_i != .															// Our entire analysis are based on those where we have information about nomination.


********************************************************************************
// Socialization pathway: Living with parents
********************************************************************************

* Place of residence
merge 1:m LopNr using "samspanel.dta", nogen keep(1 3) keepusing(SAMS year)		// Individuals	
rename SAMS sams_i
rename LopNr LopNr_BK
rename lopnrmor LopNr 

merge m:1 LopNr year using "samspanel.dta", nogen keep(1 3) keepusing(SAMS)		// Mothers
rename SAMS sams_m
rename LopNr lopnrmor
rename lopnrfar LopNr

merge m:1 LopNr year using "samspanel.dta", nogen keep(1 3) keepusing(SAMS)		// Fathers
rename SAMS sams_f
rename LopNr lopnrfar
rename LopNr_BK LopNr

save "temp1", replace


* Birthyear
merge m:1 LopNr using "Fodelseuppg.dta", nogen keep(1 3) keepusing(FodArMan Kon)
gen birthyear_i = real(substr(string(FodArMan),1,4))

rename LopNr LopNr_BK
rename lopnrmor LopNr 
merge m:1 LopNr using "Fodelseuppg.dta", nogen keep(1 3) keepusing(FodArMan Kon)
gen birthyear_m = real(substr(string(FodArMan),1,4))
rename LopNr lopnrmor

rename lopnrfar LopNr
merge m:1 LopNr using "Fodelseuppg.dta", nogen keep(1 3) keepusing(FodArMan Kon)
gen birthyear_f = real(substr(string(FodArMan),1,4))
rename LopNr lopnrfar
rename LopNr_BK LopNr

* Create county indicator 
foreach p in m f i {
	gen lan_`p' = substr(sams_`p',1,2)
}

* Calculate the share of childhood years living with parent
foreach g in lan sams {
	foreach p in mor far {
		
		* Store first letter in q
		local q = substr("`p'",1,1)
		
		* Generate dummy for whether person lives with parent
		gen `g'living`p'_thisyear = cond(`g'_i==`g'_`q',1,0,.)
		
		* Replace dummy with . if either lan/sams is missing or the person was older than 17
		replace `g'living`p'_thisyear = . if missing(`g'_i) | missing(`g'_`q') | year > birthyear_i + 17
		
		* Replace missing values through linear interpolation (affects weighting of non-missing values when average is calculated)
		bys LopNr: ipolate `g'living`p'_thisyear year, gen(`g'living`p'_imputated)

		* Calculate the share of childhood years that a person lived with the parent
		bys LopNr: egen `g'living`p'= mean(`g'living`p'_imputated)
		
		* Generate a dummy for living most of the time with the parent
		gen mostly`g'living`p' = cond(`g'living`p'>.5,1,0,.) if `g'living`p' != .

		* Clean up
		drop `g'living`p'_thisyear `g'living`p'_thisyear `g'living`p'_imputated
		
	}
}


* Keep one row per person*
bysort LopNr (year): gen unique = _n
keep if unique == 1
drop unique sams_i sams_f sams_m lan_i lan_f lan_m birthyear_i year


/*
Variables for living with nominated politican parent is defined further below.
*/


********************************************************************************
// Materialistic pathway: Income 
********************************************************************************

*Income at age 33*

merge m:1 LopNr using "Income_panel33", nogen keep(1 3)							// Individuals
rename zink i_zink
rename incpc i_incpc

rename LopNr lopNr_BK
rename lopnrmor LopNr
merge m:1 LopNr using "Income_panel33", nogen keep(1 3)							// Mothers
rename zink m_zink
rename incpc m_incpc
rename LopNr lopnrmor

rename lopnrfar LopNr
merge m:1 LopNr using "Income_panel33", nogen keep(1 3)							// Fathers
rename zink f_zink
rename incpc f_incpc
rename LopNr lopnrfar

rename lopNr_BK LopNr

save "temp2.dta", replace

********************************************************************************
// Political variables for family and extended family
********************************************************************************

* Merge in grand parents on mother's side
rename LopNr LopNr_BK
rename lopnrfar lopnrfar_BK
rename lopnrmor LopNr
merge m:1 LopNr using "BioForaldrar.dta", nogen keep(1 3) keepusing(lopnrmor lopnrfar)
rename lopnrmor lopnrmormor
rename lopnrfar lopnrmorfar
rename LopNr lopnrmor
rename *_BK *

* Merge in grand parents on father's side
rename LopNr LopNr_BK
rename lopnrmor lopnrmor_BK
rename lopnrfar LopNr
merge m:1 LopNr using "BioForaldrar.dta", nogen keep(1 3) keepusing(lopnrmor lopnrfar)
rename lopnrmor lopnrfarmor
rename lopnrfar lopnrfarfar
rename LopNr lopnrfar
rename *_BK *

* Merge in party information for parents and grand parents
foreach i in mor far mormor morfar farmor farfar {
	rename LopNr LopNr_BK
	rename firstyear firstyear_BK
	rename tophalf tophalf_BK
	rename lopnr`i' LopNr
	merge m:1 LopNr using "politicians.dta", nogen keep(1 3) keepusing(party nrinom tophalf firstyear modevaltyp)
	rename LopNr lopnr`i'
	rename party party_`i'
	rename nrinom nrinom_`i'
	rename tophalf tophalf_`i'
	rename firstyear firstyear_`i'
	rename modevaltyp modevaltyp_`i' 
	rename *_BK *
}

* Create party variables
foreach i in i mor far mormor morfar farmor farfar {
	
	* Generate variables with all missing
	foreach p in m kd fp c mp s v sd any {
		gen `p'_`i' = .
	}
	
	* Replace with 1 if candidate for that party
	replace m_`i'  = 1 if party_`i' ==   1										// Party codes from Swedish registry data
	replace kd_`i' = 1 if party_`i' ==  68
	replace fp_`i' = 1 if party_`i' ==   3
	replace c_`i'  = 1 if party_`i' ==   4
	replace mp_`i' = 1 if party_`i' ==  55
	replace s_`i'  = 1 if party_`i' ==   2
	replace v_`i'  = 1 if party_`i' ==   5
	replace sd_`i' = 1 if party_`i' == 110
	replace any_`i' = 1 if party_`i' != .
	
	* Replace with 0 if candidate for any other party
	replace m_`i'  = 0 if party_`i' !=   1 & party_`i' != .
	replace kd_`i' = 0 if party_`i' !=  68 & party_`i' != .
	replace fp_`i' = 0 if party_`i' !=   3 & party_`i' != .
	replace c_`i'  = 0 if party_`i' !=   4 & party_`i' != .
	replace mp_`i' = 0 if party_`i' !=  55 & party_`i' != .
	replace s_`i'  = 0 if party_`i' !=   2 & party_`i' != .
	replace v_`i'  = 0 if party_`i' !=   5 & party_`i' != .
	replace sd_`i' = 0 if party_`i' != 110 & party_`i' != .
	replace any_`i' = 0 if party_`i' == .
	
	* Center-right or center-left (vs all other parties (opposition, SD and "övriga partier")
	gen all_`i' = .
	replace all_`i' = 0 if party_`i' != .
	replace all_`i' = 1 if inlist(1,m_`i',kd_`i',fp_`i',c_`i')
	
	gen rg_`i' = .
	replace rg_`i' = 0 if party_`i' != .
	replace rg_`i' = 1 if inlist(1,s_`i',v_`i',mp_`i')
	
	gen big8_`i' = .
	replace big8_`i' = 0 if party_`i' != .
	replace big8_`i' = 1 if all_`i' == 1 | rg_`i' == 1 | sd_`i' == 1
	
	gen oth_`i' = .
	replace oth_`i' = 0 if party_`i' != .
	replace rg_`i' = 1 if all_`i' == 0 & rg_`i' == 0 & sd_`i' == 0
	
}

* Sums for parents and grand parents 
foreach p in m kd fp c mp s v sd any all rg big8 oth {
	egen `p'_pars = rowtotal(`p'_mor `p'_far)
	egen `p'_gpars = rowtotal(`p'_mormor `p'_morfar `p'_farmor `p'_farfar)
	gen `p'_parent = min(`p'_pars,1)
}

* Sums for siblings
foreach p in m kd fp c mp s v sd any all rg big8 oth {
	bys lopnrmor lopnrfar: egen `p'_sibs = total(`p'_i)
	replace `p'_sibs = `p'_sibs - `p'_i
	replace `p'_sibs = . if lopnrmor == . | lopnrfar == .
}

* First among the siblings
bys lopnrmor lopnrfar: egen firstyear_sibs = min(firstyear)

* Sums for all relatives (parents, siblings and grand parents)
foreach p in m kd fp c mp s v sd any all rg big8 oth {
	egen `p'_rels = rowtotal(`p'_pars `p'_gpars `p'_sibs)
}


********************************************************************************
// Materialistic pathway: income difference, percentiles
********************************************************************************

gen incpcdif = .
replace incpcdif = abs(i_incpc-m_incpc)/100 if any_mor == 1 & any_far == 0
replace incpcdif = abs(i_incpc-f_incpc)/100 if any_far == 1 & any_mor == 0


********************************************************************************
// Socialization pathway cont': Living with politician parents
********************************************************************************

foreach v in lanliving samsliving mostlylanliving mostlysamsliving {
	gen `v'pol = .
	foreach p in mor far {
		replace `v'pol = `v'`p' if any_`p' == 1
	}
}



********************************************************************************
// Other variables
********************************************************************************

* Create unique identifiers in using data
gen SenPnr = 1
gen AterPnr = 0

* Birthyear
merge 1:1 LopNr using "Fodelseuppg.dta", nogen keep(1 3) keepusing(FodArMan Kon)
gen birthyear_i = real(substr(string(FodArMan),1,4))

* Municipality for first year of running
rename firstyear year
merge 1:1 LopNr AterPnr SenPnr year using "munipanel.dta", nogen keep(1 3) keepusing(muni)



* SAMS *
merge 1:1 LopNr year using "samspanel.dta", nogen keep(1 3) keepusing(SAMS)
rename SAMS sams_i
rename year firstyear 


* Merge in municipality and SAMS for mother and father. To be used in robustness analysis
foreach i in mor far {
	rename firstyear_`i' year
	rename LopNr LopNr_BK
	rename muni muni_BK
	rename lopnr`i' LopNr
	merge m:1 LopNr AterPnr SenPnr year using "munipanel.dta", nogen keep(1 3) keepusing(muni)
	rename muni muni_`i'
	rename LopNr lopnr`i'
	rename *_BK *
	*
	merge m:1 LopNr year using "samspanel.dta", nogen keep(1 3) keepusing(SAMS)
	rename SAMS sams_`i'
	rename year firstyear_`i'
}

save "temp3.dta", replace

********************************************************************************
// Adding separate variables for number of politicians
********************************************************************************

merge 1:1 LopNr using "riksdagen.dta", nogen keep(1 3) keepusing(riksdagen)
merge 1:1 LopNr using "ko14.dta", nogen keep(1 3) keepusing(ko14)
merge 1:1 LopNr using "lt14.dta", nogen keep(1 3) keepusing(lt14)

********************************************************************************
// Create string variables
********************************************************************************

* Create string variable for party of parents
gen parentsparty = ""
gen childsparty = ""
foreach p in c fp kd m mp s v sd {
	
	* Set parentsparty
	replace parentsparty = "`p'" if `p'_mor == 1 & parentsparty == ""
	replace parentsparty = "`p'" if `p'_far == 1 & parentsparty == ""
	
	* Replace with missing if conflicting parties
	replace parentsparty = "" if `p'_mor == 1 & parentsparty != "`p'"
	replace parentsparty = "" if `p'_far == 1 & parentsparty != "`p'"
	
	* Set child's party
	replace childsparty = "`p'" if `p'_i == 1	
}


********************************************************************************
// Define variables for number of parties among relatives
********************************************************************************

* Calculate the number of political parties among parents and relatives 
foreach r in pars sibs gpars rels {
	gen parties_`r' = 0
	foreach p in $parties {
		replace parties_`r' = parties_`r' + min(1,`p'_`r') if `p'_`r' != .
	}
}

* Other variable creation
gen female = Kon - 1
gen additionalrels = big8_rels - 1 
gen firstage = firstyear - floor(FodArMan/100)


********************************************************************************
// Data preparation for main results
********************************************************************************

* Define same party, and partyshare for that party, for parties_rels == 1
gen sameparty = 0 if parties_rels == 1											/*Create a variable for running for the same political party. Main variable*/
gen partyshare = 0 if parties_rels == 1
foreach p in $parties {
	replace sameparty = 1 				if `p'_rels > 0 & `p'_rels != . & parties_rels == 1 & childsparty == "`p'" 
	replace partyshare = partyshare_`p' if `p'_rels > 0 & `p'_rels != . & parties_rels == 1
}


********************************************************************************
// Labels
********************************************************************************

foreach p in m kd fp c mp s v sd all rg {
foreach i in i parent mor far mormor morfar farmor farfar sibs gpars {

	if "`p'" == "m" local plab "Moderate"
	if "`p'" == "kd" local plab "Christian Democrat"
	if "`p'" == "fp" local plab "Liberal"
	if "`p'" == "c" local plab "Center Party"
	if "`p'" == "mp" local plab "Green Party"
	if "`p'" == "s" local plab "Social Democrat"
	if "`p'" == "v" local plab "Left party"
	if "`p'" == "sd" local plab "Sweden Democrat"
	if "`p'" == "any" local plab "Any party"
	if "`p'" == "all" local plab "Center-right"
	if "`p'" == "rg" local plab "Red-green"

	if "`i'" == "i" local ilab ""
	if "`i'" == "mor" local ilab "Mother"
	if "`i'" == "far" local ilab "Father"
	if "`i'" == "mormor" local ilab "Mother's mother"
	if "`i'" == "morfar" local ilab "Mother's father"
	if "`i'" == "farmor" local ilab "Father's mother"
	if "`i'" == "farfar" local ilab "Father's father"
	if "`i'" == "parent" local ilab "Parents"
	if "`i'" == "gpars" local ilab "Grand parents"
	if "`i'" == "sibs" local ilab "Siblings"
		
	label var `p'_`i' "`plab' `ilab'"
	
}
}
label var i_zink "Std. Income"
label var f_zink "Std. Income father"
label var m_zink "Std. Income mother"
label var all_i "CR"

label var female "Individual female"
label var any_mor "Mother politician"
label var any_gpars "Grand parents"
label var any_sibs "Siblings"
label var samslivingpol "Lived with parent (share years)"
label var mostlysamslivingpol "Lived with parent (binary, most years)"
label var lanlivingpol "Lived with parent (share years)"
label var mostlylanlivingpol "Lived with parent (binary, most years)"
label var incpcdif "Income difference"



********************************************************************************
// Finish: Keep relevant variables, order variables, compress data set
********************************************************************************

keep LopNr lopnrmor lopnrfar lopnrmormor lopnrmorfar lopnrfarfar lopnrfarmor FodArMan female additionalrels firstage parties_* *_zink incpcdif all* *_pars *_gpars *_sibs Kon big8_rels *_rels firstyear* childsparty mostly*livingpol *livingpol partyshare_* any_* big8_* tophalf muni muni_* parentsparty c_i fp_i kd_i m_i mp_i s_i v_i sd_i modevaltyp_* sams_* *_mor *_far sameparty partyshare riksdagen ko14 lt14
order LopNr lopnrmor lopnrfar lopnrmormor lopnrmorfar lopnrfarfar lopnrfarmor

compress
save "dataset.dta", replace


* Remove all unnecessary temp files from folder on server
local keep "dataset.dta"
local files : dir "" files *
local erase : list files - keep
foreach f in `erase' {
	erase "`f'"
}


*************
capture log close
clear all






